// modules/goodModules/pages/commentList/components/comment.js
import {userBehaviors} from'@/behaviors/userBehaviors'
import { msgTip } from '@/utils/MsgTipApi';
import {pubComment} from '@/api/comment'
Component({
  behaviors:[userBehaviors],
  /**
   * 组件的属性列表
   */
  properties: {
    comment:{
      type:Object,
      value:{}
    },
    index:{
      type:Number,
      value:-1
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    isPub:false, // 是否发布
    isTip:false, // 是否举报
    isAll:false, // 是否全部
    content:'',
    parentId:'',
    replyId:'',
    subComment:'',
    tipAction:[
      // {name:"举报该用户",},
      {name:"举报该评论",},
      {name:"屏蔽该评论",},
    ]
  },

  /**
   * 组件的方法列表
   */
  methods: {
    pubComment(event){
      if(!this.data.token){
        msgTip({title:"暂无登录，请先登录！"})
        return
      }
      this.setData({
        isPub:true
      })
      let parentId = event.currentTarget.dataset.id;
      let replyId = event.currentTarget.dataset.replyid || null;
      let index = event.currentTarget.dataset.index;
      this.setData({
        parentId,
        replyId,
        subComment: index != undefined ? this.data.comment.subComments[this.data.index] : this.data.comment
      })
    },
    onPubClose(){
      this.setData({
        isPub:false
      })
    },
    onIsAllClose(){
      this.setData({
        isAll:false
      })
    },
    onTipClose(){
      this.setData({isTip:false})
    },
    getAllSub(){
      this.setData({isAll:true})
    },
    async surePubComment(){
      let params = {
        content:this.data.content,
        goodId:this.data.comment.goodId,
        parentId:this.data.parentId,
        replyId:this.data.replyId,
      }
      let res =  await pubComment(params)
      this.onPubClose();
      // 评论成功后，前端数组手动添加
      if(res){
        let extendContent={
          id: res,
          likeCount:0,
          userAvatar:this.data.userInfo.avatar,
          replyUserName:this.data.subComment.userName,
          userName:this.data.userInfo.userName,
          
        }
         params = {
          ...params,...extendContent
        }
        this.triggerEvent("getPubComment",{content:params,index: this.data.index})
      }
    },
    toTip(event){
      console.log(this.data.index);
      this.setData({isTip:true})
    },
    cancleTip(){
      msgTip({title:'取消反馈'})
      this.onTipClose()
    },
    tipSelect(event){
      let {name} = event.detail;
      if(this.data.index < 0) return
      if(name == '屏蔽该评论'){
        
        this.triggerEvent('toRemoveComment',{index:this.data.index})
      }
      if(name == "举报该评论" && this.data.token){
        wx.navigateTo({
          url: `/modules/commentModules/pages/commentTip/index?commentId=${this.data.comment.id}`,
        })
      }else if(name == "举报该评论" && !this.data.token){
        msgTip({title:"请先登录~"})
      }

    }

  }
})